﻿using Microsoft.AspNetCore.Http;
using Spark.Core;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;

namespace Spark.AspNetCore
{
    public class HttpContextUser : IUser
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public HttpContextUser(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        public long UserId => long.Parse(_httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ?? "0");

        public string UserName => _httpContextAccessor.HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;

        public string MobilePhone => _httpContextAccessor.HttpContext.User.Claims.First(c => c.Type == ClaimTypes.MobilePhone).Value;

        public int UserType => int.Parse(_httpContextAccessor.HttpContext.User.Claims.First(c => c.Type == "UserType").Value);

        public IEnumerable<Claim> Claims => _httpContextAccessor.HttpContext.User.Claims;
    }
}